package xjf.learn.stream;

import org.junit.Test;
import xjf.base.constant.STATUS;
import xjf.base.entity.Employee;

import java.util.Arrays;
import java.util.Comparator;
import java.util.List;
import java.util.Optional;

/**
 * 终止操作
 *
 * 匹配：
 *      1. allMatch——检查是否匹配所有元素
 *      2. anyMatch——检查是否至少匹配一个元素
 *      3. noneMatch——检查是否没有匹配所有元素
 *
 * 查找：
 *      4. findFirst——返回第一个元素
 *      5. findAny——返回当前流中的任意元素（使用并行流效果更好，随机）
 *      6. count——返回流中元素的总个数
 *      7. max——返回流中最大值
 *      8. min——返回流中最小值
 *
 * @Author: xjf
 * @Date: 2020/4/12 21:26
 */
public class Test3 {

    /**
     * 数据准备
     */
    private List<Employee> employeeList = Arrays.asList(
            new Employee("张三", 45, 6000, STATUS.BUSY),
            new Employee("李四", 20, 7600, STATUS.FREE),
            new Employee("王五", 38, 4900, STATUS.VACATION),
            new Employee("赵六", 77, 2000, STATUS.BUSY),
            new Employee("田七", 54, 6500, STATUS.FREE),
            new Employee("田七", 54, 6500, STATUS.FREE)
    );

    /**
     * 1. allMatch
     */
    @Test
    public void test1(){
        boolean flag = employeeList.stream()
                .allMatch(employee -> STATUS.BUSY.equals(employee.getStatus()));
        System.out.println(flag);
    }

    /**
     * 2. anyMatch
     */
    @Test
    public void test2(){
        boolean flag = employeeList.stream()
                .anyMatch(employee -> STATUS.BUSY.equals(employee.getStatus()));
        System.out.println(flag);
    }

    /**
     * 3. noneMatch
     */
    @Test
    public void test3(){
        boolean flag = employeeList.stream()
                .noneMatch(employee -> STATUS.BUSY.equals(employee.getStatus()));
        System.out.println(flag);
    }

    /**
     * 4. findFirst: 短路
     */
    @Test
    public void test4(){
        Optional<Employee> first = employeeList.stream()
                .filter(employee -> {
                    System.out.println("filter");
                    return employee.getAge() > 35;
                })
                .findFirst();

        first.ifPresent(System.out::println);
    }

    /**
     * 5. findAny: 短路
     */
    @Test
    public void test5(){
        Optional<Employee> any = employeeList.stream()
                .filter(employee -> {
                    System.out.println("filter");
                    return employee.getAge() > 35;
                })
                .findAny();

        any.ifPresent(System.out::println);
    }

    /**
     * 6. count
     */
    @Test
    public void test6(){
        long count = employeeList.stream().count();

        System.out.println(count);
    }

    /**
     * 7. max
     */
    @Test
    public void test7(){
        Optional<Employee> max = employeeList.stream()
                .filter(employee -> employee.getAge() > 35)
//                .filter(employee -> employee.getAge() > 100)
                .max(Comparator.comparing(Employee::getSalary));

        max.ifPresent(System.out::println);
    }

    /**
     * 8. min
     */
    @Test
    public void test8(){
        Optional<Employee> min = employeeList.stream()
                .filter(employee -> employee.getAge() > 35)
                .min(Comparator.comparing(Employee::getSalary));

        min.ifPresent(System.out::println);
    }

}
